//	<!-- android:background="#CED8F6" --> 
package com.premedsidekick;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class ClinicalExposure extends ListActivity {
	Button button;
	ArrayAdapter<CharSequence> arrayadapter;
	Cursor cursor;
	String DATABASE_NAME = "clinical_exposure_table";
	DefaultDatabaseAdapter dbHelper;
	DefaultCursorAdapter adapter;
	
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		dbHelper = new DefaultDatabaseAdapter(this, DATABASE_NAME);
		dbHelper.open();

		cursor = dbHelper.getAll();
	
		ListView lv = getListView();
        
		button = new Button(this);
		button.setText("Insert new");
		button.setWidth(150);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick (View v) {
				dbHelper.create("Insert name", "0");
				cursor.requery();
			}
		});
		Button b = new Button(this);
		b.setText("Delete Events");
		b.setWidth(150);
		b.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick (View v) {
				dbHelper.delete();
				cursor.requery();
			}
		});
		lv.addFooterView(b);
		lv.addFooterView(button);
		
		adapter = new DefaultCursorAdapter(this, R.layout.research_layout, cursor, dbHelper);
		setListAdapter(adapter);		
	}
	public void cursorRefresh() {
		cursor.requery();
	}
	public void onDestroy() {
		super.onDestroy();
		cursor.close();
		dbHelper.close();
	}
		
	public void onStop() {
		super.onStop();
	}
/*
	public class ClinicalVolunteeringCursorAdapter extends CursorAdapter {
	    Context context;
	    int layout;
	    LayoutInflater inflater;
	    View view;
	    
	    public ClinicalVolunteeringCursorAdapter (Context context, int layout, Cursor c) {
	        super(context, c);
	        this.context = context;
	        this.layout = layout;
	        inflater = LayoutInflater.from(context);
	    }

	    @Override
	    public View newView(Context context, Cursor cursor, ViewGroup parent) {
	        view = inflater.inflate(layout, parent, false);
	        	        
	        String key = cursor.getString(0);
	        final int index = Integer.parseInt(key);
	        
	        //System.out.println("Primary Key is: " + key);
	        
	        String name = cursor.getString(1);
	        
	        final EditText edittext = (EditText)view.findViewById(R.id.edittext);
	        edittext.setText(name);

	        edittext.setOnKeyListener(new OnKeyListener() {
			    public boolean onKey(View v, int keyCode, KeyEvent event) {
			        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
			            (keyCode == KeyEvent.KEYCODE_ENTER)) {
			        	boolean temp = dbHelper.updateName(index, edittext.getText().toString());
			        	System.out.println("Save to db successful: " + temp);
			        	return true;
			        }
			        return false;
			    }
			});
	        final Spinner spinner = (Spinner) view.findViewById(R.id.spinner);
	        arrayadapter = ArrayAdapter.createFromResource(
		            context, R.array.hour_options, android.R.layout.simple_spinner_item);
	        arrayadapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
	        spinner.setAdapter(arrayadapter);
	        String hours = cursor.getString(2);                                                                  
	        int value = Integer.parseInt(hours);
	        spinner.setSelection(value);
	        spinner.setOnItemSelectedListener(new OnItemSelectedListener () {
				@Override
				public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
					Integer number = new Integer(spinner.getSelectedItemPosition());
					boolean temp = dbHelper.updateHours(index, number.toString());
					System.out.println("Save to db successful: " + temp);
				}

				@Override
				public void onNothingSelected(AdapterView<?> arg0) {
					// TODO Auto-generated method stub	
				}
	        });
	        
	        view.setOnLongClickListener(new OnLongClickListener() {

				@Override
				public boolean onLongClick(View v) {
					AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
					builder.setMessage("Are you sure you want to delete this entry?")
					       .setCancelable(false)
					       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					                boolean temp = dbHelper.delete(index);
					                System.out.println("Deletion successful: " + temp);
					                cursorRefresh();
					           }
					       })
					       .setNegativeButton("No", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					                dialog.cancel();
					           }
					       });
					AlertDialog alert = builder.create();
					alert.show();
					return true;
				}
	        });
	        return view;
	    }

	    @Override
	    public void bindView(View view, Context context, Cursor cursor) {
	    
	    	//System.out.println(cursor.getString(0) + " CHECK CHECK");
	        String key = cursor.getString(0);
	        final int index = Integer.parseInt(key);	        
	        String name = cursor.getString(1);
	        final EditText edittext = (EditText)view.findViewById(R.id.edittext);
	        //edittext = (EditText)view.findViewById(R.id.edittext);
	        
        	edittext.setText(name);

	        edittext.setOnKeyListener(new OnKeyListener() {
			    public boolean onKey(View v, int keyCode, KeyEvent event) {
			        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
			            (keyCode == KeyEvent.KEYCODE_ENTER)) {
			        	dbHelper.updateName(index, edittext.getText().toString());
			        	return true;
			        }
			        return false;
			    }
			});
	        final Spinner spinner = (Spinner) view.findViewById(R.id.spinner);
	        arrayadapter = ArrayAdapter.createFromResource(
		            context, R.array.hour_options, android.R.layout.simple_spinner_item);
	        arrayadapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
	        spinner.setAdapter(arrayadapter);
	        String hours = cursor.getString(2);                                                                  
	        int value = Integer.parseInt(hours);
	        spinner.setSelection(value);
	        spinner.setOnItemSelectedListener(new OnItemSelectedListener () {
				@Override
				public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
					Integer number = new Integer(spinner.getSelectedItemPosition());
					dbHelper.updateHours(index, number.toString());
				}

				@Override
				public void onNothingSelected(AdapterView<?> arg0) {
					// TODO Auto-generated method stub	
				}
	        });
	        view.setOnLongClickListener(new OnLongClickListener() {

				@Override
				public boolean onLongClick(View v) {
					AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
					builder.setMessage("Are you sure you want to delete this entry?")
					       .setCancelable(false)
					       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					                dbHelper.delete(index);
					                cursorRefresh();
					           }
					       })
					       .setNegativeButton("No", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					                dialog.cancel();
					           }
					       });
					AlertDialog alert = builder.create();
					alert.show();
					return true;
				}
	        });
	    }
	}
	*/

	/*
	public class ClinicalVolunteeringDatabaseHelper extends SQLiteOpenHelper {

	    static final int DATABASE_VERSION = 1;
	    static final String DATABASE_NAME = "clinical_volunteering_table";

		ClinicalVolunteeringDatabaseHelper(Context context) {
	        super(context, DATABASE_NAME, null, DATABASE_VERSION);
	    }

	    @Override
	    public void onCreate(SQLiteDatabase db) {
	        db.execSQL("CREATE TABLE " + DATABASE_NAME + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
	        		"name TEXT, hours TEXT);");
	    }

		@Override
		public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
			// TODO Auto-generated method stub
		}	
	}
	
	public class ClinicalVolunteeringDatabaseAdapter {
		public static final String KEY_ROWID = "_id";
		public static final String KEY_NAME = "name";
		public static final String KEY_HOURS = "hours";
		private static final String DATABASE_TABLE = "clinical_volunteering_table";
		private Context context;
		private SQLiteDatabase database;
		private ClinicalVolunteeringDatabaseHelper dbHelper;

		public ClinicalVolunteeringDatabaseAdapter(Context context) {
			this.context = context;
		}

		public ClinicalVolunteeringDatabaseAdapter open() throws SQLException {
			dbHelper = new ClinicalVolunteeringDatabaseHelper(context);
			database = dbHelper.getWritableDatabase();
			return this;
		}

		public void close() {
			dbHelper.close();
		}

		public long create(String name, String hours) {
			ContentValues initialValues = createContentValues(name, hours);
			return database.insert(DATABASE_TABLE, null, initialValues);
		}
		public boolean update(long rowId, String name, String hours) {
			ContentValues updateValues = createContentValues(name, hours);
			return database.update(DATABASE_TABLE, updateValues, KEY_ROWID + " = "
					+ rowId, null) > 0;
		}
		public boolean updateName(long rowId, String name) {
			ContentValues updateValues = createContentName(name);
			return database.update(DATABASE_TABLE, updateValues, KEY_ROWID + "="
					+ rowId, null) > 0;
		}
		public boolean updateHours(long rowId, String name) {
			ContentValues updateValues = createContentHours(name);
			return database.update(DATABASE_TABLE, updateValues, KEY_ROWID + "="
					+ rowId, null) > 0;
		}
		public boolean delete(long rowId) {
			return database.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
		}

		public Cursor getAll() {
			return database.query(DATABASE_TABLE, new String[] { KEY_ROWID,
					KEY_NAME, KEY_HOURS }, null, null, null,
					null, null);
		}

		public boolean delete() {
			return database.delete(DATABASE_TABLE, null, null) > 0;
		}
		private ContentValues createContentValues(String name, String hour) {
			ContentValues values = new ContentValues();
			values.put(KEY_NAME, name);
			values.put(KEY_HOURS, hour);
			return values;
		}
		ContentValues createContentName(String name) {
			ContentValues values = new ContentValues();
			values.put(KEY_NAME, name);
			return values;
		}
		ContentValues createContentHours(String hour) {
			ContentValues values = new ContentValues();
			values.put(KEY_HOURS, hour);
			return values;
		}
	}
	*/
}
